import { _decorator, Button, Component, Tween, tween, v3, Vec3, Widget } from 'cc';
import { Ab_DataTransfer_bA } from '../../../A-FRAME/A-LIB/lib.b.data';
import { Ab_AudioManager_bA } from '../../manager/audio-manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import { Ab_Popup_bA } from './popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_FlyBox_bA')
export class Ab_FlyBox_bA extends Component {
	@property(Button)
	private Ab_button_bA: Button = null;

	private Ab_va_bA: abi.Ab_cc_bA.Ab_VecAssist_bA;
	private Ab_path_bA: Vec3[];
	private Ab_calculate_vec_bA: Vec3 = v3();
	private Ab_flying_bA = false;
	private Ab_curr_index_bA = 0;
	private Ab_interval_bA = 60;
	private Ab_wgt_bA: Widget;
	private Ab_speed_bA = 100 / 0.5;

	private Ab_status_bA: 'pause'|'flying';

	onLoad() {
		this.Ab_init_bA();
	}

	start(){
		this.node.active = false;

		if(Ab_GameDC_bA.Ab_User_bA.Ab_round_bA) void 0;
		else{
			this.Ab_hide_bA();
			this.node.active = true;
		}
	}

	private Ab_init_bA(){
		this.Ab_va_bA = new abi.Ab_cc_bA.Ab_VecAssist_bA(this.Ab_button_bA.node);
		this.Ab_path_bA = abi.Ab_array_bA.Ab_create_bA(5, ()=>v3());
		this.Ab_wgt_bA = this.node.getComponent(Widget);

		Ab_DataTransfer_bA.Ab_follow_bA(Ab_GameDC_bA.Ab_Trigger_bA, 'Ab_UILuckyBox_bA', (_, show)=>{
			if(show != _){
				if(show) this.Ab_show_bA();
				else{
					this.Ab_pause_bA();
					this.Ab_hide_bA();
				}
			}
		}, false);
	}

	private Ab_onClick_bA(){
		Ab_AudioManager_bA.Ab_playEffect_bA(Ab_AudioManager_bA.Ab_EAudio_bA.Ab_EF_CLICK_bA);
		this.Ab_pause_bA();
		Ab_Popup_bA.Ab_Luckybox_bA().then(reap=>{
			if(reap){
				Ab_GameDC_bA.Ab_reapDiamond_bA(3, abi.Ab_cc_bA.Ab_canvas_bA.node.position);
				this.Ab_hide_bA();
				this.scheduleOnce(()=>this.Ab_show_bA(), this.Ab_interval_bA);
			}else this.Ab_resume_bA();
		});
	}

	private Ab_refreshWidget_bA(){
		[this.Ab_wgt_bA.top, this.Ab_wgt_bA.bottom, this.Ab_wgt_bA.left, this.Ab_wgt_bA.right] = [abi.Ab_cc_bA.Ab_is_long_screen_bA ? 590 : 490, 570, 0, 0];
		this.Ab_wgt_bA.enabled = true;
		this.Ab_wgt_bA.updateAlignment();
		this.Ab_wgt_bA.enabled = false;
	}

	private Ab_show_bA(){
		if(!this.node.active) this.node.active = true;
		this.Ab_button_bA.node.active = true;
		Tween.stopAllByTarget(this.Ab_button_bA.node);
		this.Ab_refreshWidget_bA();

		if(this.Ab_flying_bA) this.Ab_resume_bA();
		else{
			this.Ab_flying_bA = true;
			let {Ab_top_bA, Ab_bottom_bA, Ab_right_bA, Ab_left_bA} = this.Ab_va_bA;
			this.Ab_path_bA.forEach((vec, i)=>{
				vec.x = (i%2)==0 ? Ab_left_bA : Ab_right_bA;
				vec.y = abi.Ab_number_bA.Ab_random_bA(Ab_top_bA, Ab_bottom_bA, 2);
				vec.z = this.Ab_button_bA.node.position.z;
			});

			this.Ab_path_bA[0].x = this.Ab_va_bA.Ab_over_left_bA;
			this.Ab_path_bA.Ab_lastElement_bA.x = this.Ab_va_bA.Ab_over_left_bA;
			this.Ab_curr_index_bA = 0;
			this.Ab_button_bA.node.setPosition(this.Ab_path_bA[0]);
			this.Ab_next_bA();
			this.Ab_status_bA = 'flying';
		}
	}

	private Ab_hide_bA(){
		this.unscheduleAllCallbacks();
		this.Ab_flying_bA = false;
		this.Ab_button_bA.node.active = false;
	}

	private Ab_pause_bA(){
		this.Ab_status_bA = 'pause';
		Tween.stopAllByTarget(this.Ab_button_bA.node);
	}

	private Ab_resume_bA(){
		if(this.Ab_status_bA!='pause') return void 0;
		this.Ab_status_bA = 'flying';
		this.Ab_curr_index_bA--;
		this.Ab_next_bA();
	}

	private Ab_next_bA(){
		this.Ab_curr_index_bA++;
		Tween.stopAllByTarget(this.Ab_button_bA.node);

		if(this.Ab_curr_index_bA>=this.Ab_path_bA.length){
			this.Ab_hide_bA();
			this.scheduleOnce(()=>this.Ab_show_bA(), this.Ab_interval_bA);
		}else{
			let time = Vec3.subtract(this.Ab_calculate_vec_bA, this.Ab_button_bA.node.position, this.Ab_path_bA[this.Ab_curr_index_bA]).length() / this.Ab_speed_bA;
			tween(this.Ab_button_bA.node)
			.to(time, {position: this.Ab_path_bA[this.Ab_curr_index_bA]})
			.call(()=>this.Ab_next_bA()).start();
		}
	}
}

